<template>
    <view id="accountSecurity">
        <view class="block"></view>
        <view v-if="editFlag" class="form">
        	<view class="form-title">二级密码</view>
        	<input class="uni-input" disabled="true" type="password" name="pwd" maxlength="20" :value="pwd" placeholder="" />
            <view class="form-action"><button style="display: block;color: #fff;background: #1A66FF;" @click="changePwd" type="primary" size="mini">去修改</button></view>
        </view>
        <view v-else>
            <view class="form">
            	<view class="form-title">新密码</view>
            	<input class="uni-input" type="password" name="oldPassword" maxlength="20" v-model="oldPassword" placeholder="请输入新密码" />
                <view class="form-action"></view>
            </view>
            <view class="form">
            	<view class="form-title">重复密码</view>
            	<input class="uni-input" type="password" name="newPassword" maxlength="20" v-model="newPassword" placeholder="请再次输入密码" />
                <view class="form-action"></view>
            </view>
            <view class="form">
            	<view class="form-title">手机号码</view>
            	<input class="uni-input" name="phone" maxlength="11" v-model="phone" placeholder="手机号码" />
                <view class="form-action"></view>
            </view>
            <view class="form">
            	<view class="form-title">验证码</view>
            	<input class="uni-input" name="code" maxlength="6" v-model="code" placeholder="请输入验证码" />
                <view class="form-action"><button class="sendBtn" @click="sendCode" style="display: block;" type="default" size="mini">{{codeBtn.text}}</button></view>
            </view>
            <button type="primary" class="submitBtn" @click="submitFun">确认修改</button>
        </view>
    </view>
</template>
<script>
    import validate from '@/common/ys-validate.js'
    import { changeTraderPwd, sendMessage } from '@/common/api.js'
    export default {
        name: "accountSecurity",
        data() {
            return {
                pwd: "",
                editFlag:true,
                oldPassword:'',
                newPassword:'',
                phone:'',
                code:'',
                codeBtn:{
                    disabled:false,
                    text:'发送验证码'
                },
            };
        },
        components: {
        },
        methods: {
            changePwd(){
                this.editFlag=false;
            },
            submitFun(){
                if(!this.oldPassword){
                    uni.showToast({
                        icon: 'none',
                        title: '请输入原密码'
                    })
                    return;
                }
                if(!validate.isPwd(this.newPassword)){
                    uni.showToast({
                        icon: 'none',
                        title: '请输入6-20位密码'
                    })
                    return;
                }
                if(this.newPassword!=this.oldPassword){
                    uni.showToast({
                        icon: 'none',
                        title: '两次密码不一致'
                    })
                    return;
                }
                if(!validate.isPhone(this.phone)){
                    uni.showToast({
                        icon: 'none',
                        title: '请输入正确的手机号'
                    })
                    return;
                }
                if(!this.code){
                    uni.showToast({
                        icon: 'none',
                        title: '请输入验证码'
                    })
                    return;
                }
                let p = changeTraderPwd({
                    trader_pwd:this.newPassword,
                    code:this.code,
                    phone:this.phone
                }).then((res)=>{
                    const data = res.data;
                    if(data.errcode==0){
                        uni.showModal({
                            title: '提示',
                            content: '二级密码修改成功',
                            success: function (res) {
                                if (res.confirm) {
                                    uni.navigateBack({
                                        delta: 1
                                    });
                                }
                            }
                        });
                    }else if(data.errcode==1000 || data.errcode==1001){
                        uni.removeStorageSync('token');
                        uni.reLaunch({
                        	url:'/pages/home/welcome'
                        })
                    }else{
                        uni.showToast({
                        	icon: 'none',
                        	title:data.msg
                        });
                    }
                })
                return p
            },
            sendCode(){
                if(this.phone){
                    if(!validate.isPhone(this.phone)){
                        uni.showToast({
                            icon: 'none',
                            title: '请输入正确手机号'
                        })
                        return;
                    }
                    let time=60;
                    this.codeBtn.disabled=true;
                    this.codeBtn.text=`重新发送(${time})`;
                    const timer = setInterval(()=>{
                        this.codeBtn.text=`重新发送(${time--})`;
                        if(time<0){
                            this.codeBtn.disabled=false;
                            this.codeBtn.text='发送验证码'
                            clearInterval(timer)
                        }
                    },1000)
                    sendMessage({mobi:this.phone,type:'207'}).then((res)=>{
                        const data = res.data;
                        if(data.errcode==0){
                            uni.showToast({
                                icon: 'none',
                                title: '发送成功'
                            })
                        }else{
                            uni.showToast({
                                icon: 'none',
                                title: data.msg
                            })
                        }
                    })
                }else{
                    uni.showToast({
                        icon: 'none',
                        title: '请先输入手机号'
                    })
                }
            },
        },
        onLoad() {
            let data=uni.getStorageSync('_walletInfo_');
            if(data.trader_pwd==1){
                this.pwd='***********';
                this.pwdText='去修改'
            }else{
                this.editFlag=false;
            }
        }
    };
</script>
<style lang="scss">
    #accountSecurity {
        position: relative;
        height: 100%;
        .block{
            height: 24rpx;
        }
        .form{
            display: flex;
            align-items: center;
            justify-content: center;
            flex: 1;
            background: #fff;
            .form-title{
                padding: 20rpx;
                padding-left: 30rpx;
                width: 150rpx;
            }
            .form-action{
                padding: 10rpx 30rpx;
                .sendBtn{
                    color: #000;
                }
            }
        }
        .submitBtn{
            margin: 30rpx;
            font-size: 32rpx;
            margin-top: 60rpx;
            border-radius: 30rpx;
            color: #fff;
            background: #1A66FF;
        }
    }
</style>
